package project.houseRent.view;

import project.houseRent.ScannerUtils;
import project.houseRent.domain.House;
import project.houseRent.service.HouseService;

import java.util.Scanner;
/**
 *
 * todo: 回车不知道怎么才能不修改
 * todo: scanner in 相关的数据不知道怎么封装
 */

/**
 * 用户交互界面（主菜单）
 */
public class HouseView {
    private HouseService houseService;
    //自动递增的房屋id
    private int houseId = 1;

    /**
     * 用户选择主菜单
     */
    public void mainMenu() {
        houseService = new HouseService(2);
        boolean flag = true;
        while (flag) {
            System.out.println("===========房屋出租系统===========");
            System.out.println("1. 新增房屋");
            System.out.println("2. 查找房屋");
            System.out.println("3. 删除房屋");
            System.out.println("4. 修改房屋信息");
            System.out.println("5. 房屋列表");
            System.out.println("6. 退出");
            System.out.println("请选择（1-6）：");
            char choice = ScannerUtils.readMenuSelection();
            switch (choice) {
                case '1':
                    House house = new House();
                    house.setId(houseId++);
                    System.out.println("-------------添加房屋-------------");
                    System.out.print("姓名：");
                    house.setName(ScannerUtils.readString(5));
                    System.out.print("电话：");
                    house.setPhone(ScannerUtils.readString(11));
                    System.out.print("地址：");
                    house.setAddress(ScannerUtils.readString(100));
                    System.out.print("月租：");
                    house.setRent(ScannerUtils.readInt());
                    System.out.print("状态：");
                    house.setState(ScannerUtils.readString(3));
                    houseService.createHouse(house);
                    System.out.println("-------------添加完成-------------");
                    break;
                case '2':
                    System.out.println("-------------查找房屋-------------");
                    System.out.print("请输入你要查找的id：");
                    int findId = ScannerUtils.readInt();
                    House house1 = houseService.findById(findId);
                    if (house1 == null) {
                        System.out.println("----------没有找到该房屋----------");
                    } else {
                        System.out.println(house1);
                    }
                    break;
                case '3':
                    System.out.println("-------------删除房屋-------------");
                    System.out.print("请输入你要删除的房屋id：");
                    int delId = ScannerUtils.readInt();
                    System.out.print("是否确定删除？Y/N");
                    char choice1 = ScannerUtils.readChar();
                    if (choice1 == 'y') {
                        houseService.deleteHouse(delId);
                    }
                    break;
                case '4':
                    System.out.println("-------------修改房屋信息-------------");
                    System.out.print("请输入要修改的房屋id：");
                    int updateId = ScannerUtils.readInt();
                    House house2 = houseService.findById(updateId);
                    if(house2==null){
                        System.out.println("----------没有找到该房屋----------");
                        break;
                    }
                    System.out.print("姓名：" + "(" + house2.getName() + "): ");
                    //如果用户直接输入回车，则不设置保持原来的
                    String updateName = ScannerUtils.readString(10, true);
                    //输入内容我们才设置值，否则不做处理
                    if(!"".equals(updateName) || !updateName.trim().isEmpty()){
                        house2.setName(updateName);
                    }
                    System.out.print("电话：" + "(" + house2.getPhone() + "): ");
                    String updatePhone = ScannerUtils.readString(11, true); // 改为nextLine以匹配上一个nextLine的行为模式
                    if (!updatePhone.trim().isEmpty()) {
                        house2.setPhone(updatePhone);
                    }
                    System.out.print("地址："+"("+house2.getAddress()+"): ");
                    String updateAddress = ScannerUtils.readString(100, true);
                    if(!updateAddress.equals("")){
                        house2.setAddress(updateAddress);
                    }
                    System.out.print("租金："+"("+house2.getRent()+"): ");
                    int updateRent = ScannerUtils.readInt();
                    if(updateRent!=0){
                        house2.setRent(updateRent);
                    }
                    System.out.print("状态："+"("+house2.getState()+"): ");
                    String updateState = ScannerUtils.readString(3, true);
                    if(!updateState.equals("")){
                        house2.setState(updateState);
                    }
                    houseService.updateHouse(house2);
                    System.out.println("------------修改完成------------");
                    break;
                case '5':
                    houseService.list();
                    break;
                case '6':
                    System.out.println("已退出...");
                    flag = false;
                    break;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
        }
    }
}
